package com.kxf.recyclerviewtest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

/**
 * @ProjectName: AndroidTestDemo
 * @Package: com.kxf.recyclerviewtest
 * @ClassName: StateListenRecyclerView
 * @Description: java类作用描述
 * @Author: kuangxuefeng
 * @qq: 1024883177
 * @CreateDate: 2020/11/4 15:35
 */
public class StateListenRecyclerView extends RecyclerView {

    private static final String TAG = "StateListenRecyclerView";
    private float action_down_y = 0;
    private float max_down_y = 0;
    private ArrayList<StateListener> stateListeners = new ArrayList<>();

    private enum state{

    }

    private enum Pos{
        top,center, bottom
    }

    private boolean isRefshing = false;
    protected int lastState = 0;//
    private Pos lastPos = Pos.top;

    public StateListenRecyclerView(@NonNull Context context) {
        super(context);
        init();
    }

    public StateListenRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StateListenRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (!canScrollVertically(1)) {
                    if (lastPos != Pos.bottom){
                        Log.d(TAG, "滑动到底部了");
                        lastPos = Pos.bottom;
                        for (StateListener stateListener : stateListeners){
                            stateListener.onScrollToBottom();
                        }
                    }
                }
                // canScrollVertically(-1) 为false 的时候滑动到顶部了
                if (!canScrollVertically(-1)) {
                    if (lastPos != Pos.top){
                        Log.d(TAG, "滑动到顶部了");
                        lastPos = Pos.top;
                        for (StateListener stateListener : stateListeners){
                            stateListener.onScrollToTop();
                        }
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!isRefshing && lastPos == Pos.top && dy>10){
                    lastPos = Pos.center;
                    Log.d(TAG, "开始上滑");
                    for (StateListener stateListener : stateListeners){
                        stateListener.onStartScrollToUp();
                    }
                }else if (!isRefshing && lastPos == Pos.bottom && dy<-10){
                    Log.d(TAG, "开始下滑");
                    lastPos = Pos.center;
                    for (StateListener stateListener : stateListeners){
                        stateListener.onStartScrollToDown();
                    }
                }
//                Log.d(TAG, "onScrolled===>>>" + dy);
            }
        });
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                action_down_y = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                if (lastPos == Pos.top && ev.getY()-action_down_y>50){
                    if (!isRefshing){
                        isRefshing = true;
                        Log.d(TAG, "准备下拉刷新");
                        for (StateListener stateListener : stateListeners){
                            stateListener.onRefshPre();
                        }
                    }else {
                        max_down_y = (ev.getY()-action_down_y > max_down_y)?(ev.getY()-action_down_y):max_down_y;
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (isRefshing){
                    if (lastPos == Pos.top && ev.getY()-action_down_y>max_down_y-50){
                        Log.d(TAG, "完成下拉刷新");
                        for (StateListener stateListener : stateListeners){
                            stateListener.onRefshFinish();
                        }
                    }else {
                        Log.d(TAG, "取消下拉刷新");
                        for (StateListener stateListener : stateListeners){
                            stateListener.onRefshCancel();
                        }
                    }
                    isRefshing = false;
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    public void addStateListeners(StateListener stateListener){
        stateListeners.add(stateListener);
    }

    public interface StateListener{
        void onScrollToTop();
        void onScrollToBottom();
        void onRefshPre();
        void onRefshFinish();
        void onRefshCancel();
        void onStartScrollToDown();
        void onStartScrollToUp();
    }
}
